package qj.ps;

import java.io.IOException;
import java.sql.*;
import java.util.*;
import java.util.Map.Entry;

import org.apache.commons.codec.binary.Base64;

public class DbStatus {
	public static void main(String[] args) {
		System.out.println(new String(Base64.decodeBase64("VkZURVNUOlZGVEVTVA==")));
	}
	public static void main1(String[] args) throws Exception {
		testDBCounts();
		
		//new File("/home/quanle/Workon/assembler/manifest-work/sla/dev/er_core_batch_12-1-BETA_03012012/appserver/er-core-batch-domain/applications/ercore.ear/APP-INF/lib/ER_api_CR_1978_test2.jar")
		
		
		
	}

	private static void testDBCounts() throws ClassNotFoundException,
			SQLException, IOException {
		Class.forName("oracle.jdbc.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","er","er");
		DatabaseMetaData metaData = conn.getMetaData();
		
		List<String> tables = getStrings(metaData.getTables(conn.getCatalog(), "ER", "ER_%", new String[] {"TABLE"}),3);
//		System.out.println(tables);
//		Map<String, Integer> oldCounts = getCounts(tables,conn);
//		System.out.println(oldCounts);
//		System.out.println("Waiting...");
//		new BufferedReader(new InputStreamReader(System.in)).readLine();
////		System.console().readLine();
//		Map<String, Integer> newCounts = getCounts(tables,conn);
//		System.out.println(newCounts);
//		System.out.println(compare(newCounts, oldCounts));
	}

	public static Map<String, Integer> compare(Map<String, Integer> counts,
			Map<String, Integer> oldCounts) {
		TreeMap<String, Integer> ret = new TreeMap<String, Integer>();
		for (Entry<String, Integer> entry : counts.entrySet()) {
			Integer oldVal = oldCounts.get(entry.getKey());
			Integer newVal = entry.getValue();
			if (!oldVal.equals(newVal)) {
				ret.put(entry.getKey(), newVal - oldVal);
			}
		}
		return ret;
	}

	static Map<String,Integer> getCounts(List<String> tables, Connection conn) {
		TreeMap<String, Integer> ret = new TreeMap<String, Integer>();
		for (String table : tables) {
			ret.put(table, count(table, conn));
		}
		
		return ret;
	}

	public static int count(String table, Connection conn) {
		try {
			PreparedStatement st = conn.prepareStatement("select count(*) from " + table);
			ResultSet rs = st.executeQuery();
			rs.next();
			return rs.getInt(1);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	static List<String> getStrings(ResultSet rs,int columnIndex) {
		LinkedList<String> ret = new LinkedList<String>();
		try {
//			printColumnNames(rs);
			while (rs.next()) {
				ret.add(rs.getString(columnIndex));
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return ret;
	}

	@SuppressWarnings("unused")
	private static void printColumnNames(ResultSet rs) {
		try {
			ResultSetMetaData rsMetaData = rs.getMetaData();
			for (int i = 0; i < rsMetaData.getColumnCount(); i++) {
				System.out.println(rsMetaData.getColumnName(i + 1));
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
